//
//  OnLieTeacherDetailListVC.m
//  GC_OC
//
//  Created by lbh on 2022/4/8.
//

#import "OnLieTeacherDetailListVC.h"
#import "OnlineEducationHeaderView.h"
#import "EducationTableViewCell.h"
#import "TearchFengCaiView.h"
#import "EducationTabCell.h"
#import "SdScrollView.h"
#import "TearchFengCaiView.h"
#import "YBPopupMenu.h"
#import "OnlineSubMenuVC.h"
#import "OnlineTopHeadView.h"
#import "OnLineTeacherDetailVC.h"
#import "EmptyView.h"

@interface OnLieTeacherDetailListVC ()<UITableViewDelegate,UITableViewDataSource,WMZPageProtocol>
@property (nonatomic, strong)OnlineEducationHeaderView* headerView;
@property (nonatomic, strong)ObjectAFNetWorking* objectAFNet;
@property (nonatomic, strong)NSDictionary* dic;
@property (nonatomic, strong)UITableView* tableview;
@property (nonatomic,strong)TearchFengCaiView *teacherFCView;
@property (nonatomic,copy)NSString *currenClassID;//当前选择的班级ID
@property (nonatomic, strong)NSDictionary* currenClassDict;
@property (nonatomic,copy)NSString *classTitle;
@property (nonatomic,strong)OnlineTopHeadView *topSearchHeadView;
@property (nonatomic,strong)NSArray *classListArray;
@property (nonatomic,strong)EmptyView *emptyView;

@end

@implementation OnLieTeacherDetailListVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.objectAFNet=[[ObjectAFNetWorking alloc]init];
    self.view.backgroundColor=RBG(246,247,249);
    self.view.backgroundColor = UIColor.whiteColor;
    
    _tableview=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableview.dataSource=self;
    _tableview.delegate=self;
    _tableview.showsVerticalScrollIndicator = NO;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    [_tableview registerClass:[EducationTabCell class] forCellReuseIdentifier:@"EducationTabCell"];
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableview registerClass:[EducationTitleTabCell class] forCellReuseIdentifier:@"EducationTitleTabCell"];
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    //    self.tableview.backgroundColor = RBG(246,247,249);
    self.tableview.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:_tableview];
    if (@available(iOS 11.0, *)) {
        
        _tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        
        self.automaticallyAdjustsScrollViewInsets = NO;
        self.edgesForExtendedLayout = UIRectEdgeAll;
    }
    if (@available(iOS 15.0, *)) {
        [_tableview setValue:@0 forKey:@"sectionHeaderTopPadding"];
    }
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(ISIphoneX?-0:0);
    }];
    
//    [self getClassContentData];
    
    self.emptyView.hidden = YES;
    self.emptyView.bEmptyLabel.text = @"暂无数据";
    self.emptyView.bEmptyImageView.image = [UIImage imageNamed:@"无数据"];
    [self.view addSubview:self.emptyView];
    [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(100);
    }];
}
- (NSString *)timeFormatted:(int)totalSeconds{
    
    int seconds = totalSeconds % 60;
    int minutes = (totalSeconds / 60) % 60;
    return [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];
}
////实现协议 悬浮 必须实现
- (UIScrollView *)getMyScrollView{
    return self.tableview;
}

#pragma mark --NetWork
//更加课程ID获取目录内容
-(void)getClassContentData{
    self.emptyView.hidden = YES;
    [self.objectAFNet getUrlStr:[NSString stringWithFormat:@"%@/api/home/getcatelist?id=%@&page=%@&pagesize=%@",server_addr,self.medicallID,@"1",@"10000"] finish:^(id  _Nonnull dataString) {
        NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:nil];
        NSArray *list = dic[@"data"][@"list"];
        list = [[list reverseObjectEnumerator] allObjects];
        self.classListArray = list;
        [self.tableview reloadData];
        if (self.classListArray.count == 0) {
            self.emptyView.hidden = NO;
            [[NSNotificationCenter defaultCenter]postNotificationName:@"changeLineShow" object:nil];
        }
    } andError:^(NSError * _Nonnull error) {
        self.emptyView.hidden = NO;
        [self.view hideBusyHUD];
    }];
}
#pragma mark --TableViewDelete
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    if (section == 1) {
//        return self.classListArray.count;
//    }
//    else{
        return self.classArray.count;
//    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 00)];
    footView.backgroundColor = UIColor.greenColor;
    return footView;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
    if (section ==0) {
        return nil;
    }
    else{
        UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
        headView.backgroundColor = RBG(246,247,249);
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 20)];
        lineView.backgroundColor = AppMainColor;
        lineView.layer.masksToBounds = YES;
        lineView.layer.cornerRadius = 2.5;
        [headView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(headView.mas_centerY);
            make.left.mas_equalTo(wid(20));
            make.width.mas_equalTo(5);
            make.height.mas_equalTo(20);
        }];
        
        UILabel* tip1Label=[[UILabel alloc]init];
        tip1Label.text = @"所有课程";
        tip1Label.textColor=[UIColor blackColor];
        tip1Label.font=[UIFont boldSystemFontOfSize:14];
        [headView addSubview:tip1Label];
        [tip1Label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(lineView.mas_centerY);
            make.left.mas_equalTo(lineView.mas_right).offset(10);
        }];
        
//        UIImageView* imageView=[[UIImageView alloc]init];
//        imageView.image = [UIImage imageNamed:@"logo_right_jt"];
//        [headView addSubview:imageView];
//        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerY.mas_equalTo(lineView.mas_centerY);
//            make.right.mas_equalTo(-wid(20));
//            make.size.mas_equalTo(CGSizeMake(15, 15));
//        }];
        return headView;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
    if (section == 0) {
        return 0;
    }
    else{
        return 50;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = nil;
    EducationTitleTabCell* cell=[tableView dequeueReusableCellWithIdentifier:@"EducationTitleTabCell" forIndexPath:indexPath];
//    if (indexPath.section == 0) {
//        dict = self.classArray[indexPath.row];
//    }
//    else{
//        dict = self.classListArray[indexPath.row];
//    }
    dict = self.classArray[indexPath.row];
    NSString *image = dict[@"image"];
    NSString *name = dict[@"name"];
    NSString *describe = dict[@"describe"];
    NSString *audio_num = dict[@"audio_num"];
    NSString *video_num = dict[@"video_num"];
    int video_numInt = video_num.intValue;
    int audio_numInt = audio_num.intValue;
    NSString * sNum = @"00:00";
    if (audio_numInt == 0 && video_numInt == 0) {
        sNum = @"00:00";
    }
    else if (audio_numInt != 0){
        sNum = [self timeFormatted:audio_numInt];
    }
    else if (video_numInt != 0){
        sNum = [self timeFormatted:video_numInt];
    }
    cell.titleLabel.text = name;
    cell.timeLabel.text = sNum;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CAShapeLayer *maskLayer = [UIView createBezierPath:CGRectMake(0, 0, SCREEN_WIDTH, 50) withRoundingCorners: UIRectCornerBottomRight | UIRectCornerBottomLeft | UIRectCornerTopRight | UIRectCornerTopLeft withRadid:CGSizeMake(0, 0)];
    cell.bgView.layer.mask = maskLayer;
    if (self.classArray.count != 1) {
        if (indexPath.section == 0 && indexPath.row == self.classArray.count -1 ) {
            CAShapeLayer *maskLayer = [UIView createBezierPath:CGRectMake(0, 0, SCREEN_WIDTH, 50) withRoundingCorners: UIRectCornerBottomRight | UIRectCornerBottomLeft withRadid:CGSizeMake(25, 25)];
            cell.bgView.layer.mask = maskLayer;
        }
        if (indexPath.section == 1 && indexPath.row == 0 ) {
            CAShapeLayer *maskLayer = [UIView createBezierPath:CGRectMake(0, 0, SCREEN_WIDTH, 50) withRoundingCorners: UIRectCornerTopLeft | UIRectCornerTopRight withRadid:CGSizeMake(25, 25)];
            cell.bgView.layer.mask = maskLayer;
        }
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = nil;
    if (indexPath.section == 0) {
        dict = self.classArray[indexPath.row];
        CurriculumModel *topModel = [[CurriculumModel alloc] initWithDic:dict];
        NSMutableArray *contentArray = [[NSMutableArray alloc]init];
        for (NSDictionary *dictt in self.classArray) {
            CurriculumModel *topModel = [[CurriculumModel alloc] initWithDic:dictt];
            [contentArray addObject:topModel];
        }
        NSLog(@"self.classArray~~>>%@",self.classArray);
        [[PlayModel sharedModel] play:topModel listArr:contentArray viewPlayY:SCREEN_HEIGHT -  50 - HY_BottomWhiteBar_Height];
        //        [[GCAVPlayer shared] play:self.classArray andindex:indexPath.row ViewStatue:0 andIsAudio:NO];
    }
//    else{
//        dict = self.classListArray[indexPath.row];
//        CurriculumModel *topModel = [[CurriculumModel alloc] initWithDic:dict];
//        NSMutableArray *contentArray = [[NSMutableArray alloc]init];
//        for (NSDictionary *dictt in self.classListArray) {
//            CurriculumModel *topModel = [[CurriculumModel alloc] initWithDic:dictt];
//            [contentArray addObject:topModel];
//        }
//        [[PlayModel sharedModel] play:topModel listArr:contentArray];
//    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat y = scrollView.contentOffset.y;
    if (y <= 0) {
        self.tableview.contentOffset = CGPointMake(0, 0);
    }
}
//空视图
- (EmptyView *)emptyView {
    
    if (!_emptyView) {
        
        _emptyView = [[EmptyView alloc]init];
        _emptyView.backgroundColor = [UIColor clearColor];
        _emptyView.hidden = YES;
    }
    return _emptyView;
}
@end
